if (!window.XMLHttpRequest) XMLHttpRequest = function()
{
  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e){}
  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e){}
  try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e){}
  throw new Error("Could not find an XMLHttpRequest alternative.")
};

// Function comes from Google App Engine article 
// <http://code.google.com/appengine/articles/rpc.html>
function Request(function_name,opt_argv,action_str) 
{
  if (!opt_argv)
  {
    opt_argv = new Array();
  }

  var callback = null;
  var len = opt_argv.length;

  if (len > 0 && typeof opt_argv[len-1] == 'function')
  {
    callback = opt_argv[len-1];
    opt_argv.length--;
  }
  var async = (callback != null);
  var params = new Array(function_name);

  for (var i = 0; i < opt_argv.length; i++)
  {
    params.push(opt_argv[i]);
  }
  var body = JSON.stringify(params);
  var req = new XMLHttpRequest();
  req.open('POST', action_str, async);

  req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  req.setRequestHeader("Content-length",body.length);
  req.setRequestHeader("Connection","close");


  if (async) 
  {
    req.onreadystatechange = function()
    {
	 if(req.readyState == 4 && req.status== 200)
	 {
      var response = null;
      try
      {
        response = JSON.parse(req.responseText);
      }
      catch(e)
      {
        response = req.responseText;
      }
      callback(response);
	  }
    }
  }

  req.send(body);
}

function InstallFunction(obj,functionName)
{
  obj[functionName] = function() { Request(functionName,arguments,'/rpc'); }
}

var server = {};
InstallFunction(server,'getCollection');
InstallFunction(server,'AddUsage');

function add_use(endeavor_key)
{
  server.AddUsage(endeavor_key);
}

function DisplayCollection(filter)
{
  server.getCollection(filter,DisplayCollectionSuccess);
}

function DisplayCollectionSuccess(response)
{
  var output = '';
  var content = document.getElementById('main-content');
  if (response)
  {
    var collection = response['collection'];
	var filter = '';
	if (response['filter_type'].length < 2)
	{
	  filter = response['filter_type'].toUpperCase();
	} else {
	  filter = response['filter_type'];
	}
	output += '<h2>E-Library Resources for ' + filter + '</h2>';
	output += '<dl>';
	for(var i=0;i < collection.length;i++)
    {
     var endeavor = response['collection'][i];
	 output += GenerateRecordDisplay(endeavor);
    }
	output += '</dl><span class="money"><img src="http://www.western.edu/favicon.ico" /></span> ';
	output += '- purchased by Western State College, restricted access.'
  } else {
    output += 'No items found';
  }
  content.innerHTML = output;
}

function expandDescription(anchor,target_div_id)
{
  desc_div = document.getElementById(target_div_id);
  if (desc_div.style.visibility == 'hidden') {
    desc_div.style.visibility = 'visible';
    desc_div.style.display='block';
  } else {
    desc_div.style.visibility = 'hidden';
    desc_div.style.display='none';
  }
}


function GenerateRecordDisplay(endeavor)
{
  var output = '<dt><a target="_top" href="' + endeavor['url'] + '">' + endeavor['title'] + "</a></dt>";
  if(endeavor['is_subscription'])
  {
    output += '<span class="money"><img src="http://www.western.edu/favicon.ico" /></span>';
  }
  output += '<dd>' + endeavor['description'] + '</dd>'
  return output;
}
